// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/common/exception/exception.h>

namespace kumo::pollux::dwio::common {
    /**
     * Error tolerance level for readers
     * Errors include schema mismatch(not able to parse data as expected type)
     */
    enum class ErrorToleranceLevel {
        TOLERANCE_IGNORE, // Ignore any column parsing errors.
        TOLERANCE_STRICT, // Raise exception immediately
        TOLERANCE_PERMISSIVE // Allow user specified number of conversion errors.
        // bad values are converted to nulls
    };

    struct ErrorTolerance {
    private:
        ErrorToleranceLevel toleranceLevel_ =
                ErrorToleranceLevel::TOLERANCE_PERMISSIVE;
        float maxAllowableErrorsRatio_ = 1;

    public:
        ErrorTolerance() = default;

        void setTolerance(
            ErrorToleranceLevel toleranceLevel,
            float maxAllowableErrorsRatio = 1) {
            toleranceLevel_ = toleranceLevel;
            maxAllowableErrorsRatio_ = maxAllowableErrorsRatio;
            DWIO_ENSURE(
                ErrorTolerance::maxAllowableErrorsRatio_ > 0 &&
                ErrorTolerance::maxAllowableErrorsRatio_ <= 1,
                "invalid Error tolerance: ",
                maxAllowableErrorsRatio);
        }

        ErrorToleranceLevel toleranceLevel() {
            return toleranceLevel_;
        }

        float maxAllowableErrorsRatio() {
            return maxAllowableErrorsRatio_;
        }
    };
} // namespace kumo::pollux::dwio::common
